#!/bin/sh

# fail if any commands fails
set -e
# debug log
#set -x

show_usage () {
    printf "Usage: $0 [options [parameters]]\n"
    printf "\n"
    printf "Mandatory options:\n"
    printf " -i|--iwad          [doom|doom2|tnt|plutonia|heretic|hexen]\n"
    printf " -l|--map-limit     [none|vanilla|nolimit|boom|zdoom]\n"
    printf "\n"
    printf "Options:\n"
    printf " -d|--game-dir      [/path/to/doom/base/directory] (Optional, default: '~/games/doom')\n"
    printf " -h|--help, Print help\n"

exit
}

if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
  show_usage
fi
if [ -z "$1" ]; then
  show_usage
fi

while [ -n "$1" ]; do
  case "$1" in
    --game-dir|-d)
      shift
      param_game_dir=$1
      ;;
    --iwad|-i)
      shift
      param_iwad=$1
      ;;
    --map-limit|-l)
      shift
      param_map_limit=$1
       ;;
    *)
      show_usage
      ;;
  esac
shift
done

### Configuration
if [ -z "$param_game_dir" ]; then
      param_game_dir=$HOME/games/doom
fi
config_script_dir="$(pwd $(dirname $0))"
config_iwads_dir=$param_game_dir/wads/iwads


### check parameter values
# iwad
doom_game="doom doom2"
echo "$doom_game" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_iwad" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_iwad is not a valid option, valid options are: $doom_game"; exit 1; fi; rm match

map_limit="none vanilla nolimit boom zdoom"
echo "$map_limit" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_map_limit" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_map_limit is not a valid option, valid options are: $map_limit"; exit 1; fi; rm match


### Script
config_exclude_files="! -name *tex*.* ! -name *res*.* ! -name *fix.* ! -name *demo*.* ! -name *credits*.*"
config_pwads_dir="$param_game_dir/wads/$param_iwad"
if [ "$param_map_limit" = "none" ]; then
    config_pwad_file=$(find "$config_pwads_dir"/vanilla "$config_pwads_dir"/nolimit "$config_pwads_dir"/boom "$config_pwads_dir"/zdoom/ -name "*.wad" -or -name "*.pk3" ${config_exclude_files} -type f 2>/dev/null | shuf -n 1)
elif [ "$param_map_limit" = "vanilla" ]; then
    config_pwad_file=$(find "$config_pwads_dir"/vanilla -name "*.wad" ${config_exclude_files} -type f 2>/dev/null | shuf -n 1)
elif [ "$param_map_limit" = "nolimit" ]; then
    config_pwad_file=$(find "$config_pwads_dir"/nolimit -name "*.wad" ${config_exclude_files} -type f 2>/dev/null | shuf -n 1)
elif [ "$param_map_limit" = "boom" ]; then
    config_pwad_file=$(find "$config_pwads_dir"/boom -name "*.wad" ${config_exclude_files} -type f 2>/dev/null | shuf -n 1)
elif [ "$param_map_limit" = "zdoom" ]; then
    config_pwad_file=$(find "$config_pwads_dir"/zdoom/ -name "*.wad" -or -name "*.pk3" ${config_exclude_files} -type f 2>/dev/null | shuf -n 1)
fi


# Check pwad found
if [ -z "$config_pwad_file" ]; then
  echo "ERROR: No pwad file found"
  echo "ERROR: param_iwad        : $param_iwad"
  echo "ERROR: pwad file         : $config_pwad_file"
  exit 1
else
  echo "$config_pwad_file"
fi